package com.zxy.filter;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter
@Slf4j
public class TraceIdFilter implements Filter {
    public static String TRACEID = "traceId";
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request =  (HttpServletRequest) servletRequest;
        String traceId = request.getHeader(TRACEID);
        if(ObjectUtil.isNotEmpty(traceId)){
            MDC.put(TRACEID,traceId);
        }
        filterChain.doFilter(servletRequest,servletResponse);

    }
}
